#Hi, how can i turn the gold spawn script to local script?

1 messages · Page 1 of 1 (latest)

compact coyote
#

Hi i made a gold spawn script its spawns yellow gold parts every 10 seconds and i put that script in some kind of npcs (every players has it own npcs) so i want golds to seen only by the client (everyone needs to see its own gold parts) for better optimization but i heard if i do the spawn thing in local script that it will be open for exploits how can i solve this problem? thank you.

rich sky
#

What i would do is, create on Server side a Position table, or UUID, idk which would be best approch.
Like

goldSpawn = {}

-- Create a part, and get position, and save it.
goldSpawn[pos] = part
-- then use FireClient with position

On client side, i would just use the simple mechanics, such touch -> fire server position -> Destroy
Oh and i forgot, of course listen the event, and spawn the part in the excat giving position from server.

Then once again on Server side, check if position exists in the table, if its so. Delete it from table and reward player.

compact coyote
# rich sky What i would do is, create on Server side a Position table, or UUID, idk which w...

Thank you for your answer its giving me ideas but there is a problem with that the gold doesnt spawn anchored im spawning the gold random place near the npcs head and im giving it a random velocity like this part.AssemblyLinearVelocity = Vector3.new(math.random(-15, 15), math.random(20, 30), math.random(-15, 15)) so i cant really know where it is because it falling when it spawned, thank you.

#

i can send the script if you're interested.

sturdy pine
#

Put .anchored = true

rich sky
#

If you don't want to anchor it, then using UUID is the alternativ way, you generate the UUID on server, and save it instead Pos. And also sent the UUID to client, In client, you making a table too, like goldParts[uuid] = part. , if touched, destroy and sent the UUID instead pos. (Table is really not needed in client, i think.)

compact coyote
compact coyote
#

That way is safe for exploits and optimized right?

rich sky
#

Yes, since you create UUID on server side with a tables, let's say you have 10 parts in Server table like

goldSpawn[player.UserID] = {
    4774182-4f72-4f34-a84a-e92df2ddeffa = part,
    917ca813-e717-43d6-8d27-5d59be754f27 = part,
    -- and so going on
}

And you sent the UUID to client, if player touched it, sent it again to server

so server check if

goldSpawn[player.UserID][uuid] exists, if so, reward player, if not, then it doesn't exists, so it means player exploits for sure.
Player can create UUID on client, and sent it, but then Server doesn't recgonize this UUID, since the Server didn't create this UUID at all.

So in end, there is kinda "low" chance that player can exploits it.
Player still can exploit it, by getting the UUID and using fireServer without touching, so if you wanna make it more non exploits, make another layer of security, by checking if Player is really close to the part (touching).

i hope i explained it very well.

compact coyote
rich sky
#

Youre welcome, since im was boring. This is what i would do. Maybe it will help you guide the path.

Code below, if you wanna see it:
||

-- Localscript
local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent


RemoteEvent.OnClientEvent:Connect(function(uuid: string)
    local part = Instance.new("Part")
    part.Size = Vector3.new(1,1,1)
    part.CFrame = CFrame.new(math.random(0,100),10,math.random(0,100))
    part.Parent = workspace
    
    local debounce = false
    
    part.Touched:Connect(function(hit)
        if debounce then return end
        if hit.Parent ~= game.Players.LocalPlayer.Character then return end
        debounce = true
        RemoteEvent:FireServer(uuid)
        part:Destroy()
    end)
end)
-- Server
local http = game:GetService("HttpService")
local players = game:GetService("Players")
local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent

local goldSpawn = {}
local amount = 0 -- of course since im singleplayer, you might using another way to spawn it

players.PlayerAdded:Connect(function(ply : Player)
    local userID = ply.UserId
    goldSpawn[userID] = {}
    while true do
        wait(1)
        if amount == 10 then continue end
        local uuid 
        repeat uuid = http:GenerateGUID(false) until not goldSpawn[userID][uuid]
        -- Either create Pos here, or on Client side, for more safety of course, create Location here.
        goldSpawn[userID][uuid] = true
        RemoteEvent:FireClient(ply, uuid)
        amount += 1
    end
end)

RemoteEvent.OnServerEvent:Connect(function(ply: Player, uuid: string)
    if not goldSpawn[ply.UserId][uuid] then
        warn(ply.Name.. " might trying to explotis.")
        return
    end
    goldSpawn[ply.UserId][uuid] = nil
    print(ply.Name.." should be rewarded by 1 Coins.")
end)

||

compact coyote
#

Thanks for the code now i exactly know what i should do

rich sky
#

👍

compact coyote
# rich sky 👍

I'm redoing all my scripts because I now realize they weren't very optimized. I never thought about optimizing my game before, lol

rich sky
#

Its fine, its usally how people start with "messy script", and later on they redo it to make it more cleaner, but maybe still not optimizing enough. keeek
i belive mine is still not enough optimizing.

compact coyote