OnTouch script run locally?

I have an a game where eggs spawn throughout a baseplate. Each egg has an on-touch script in it. I’m trying to make the eggs spawn locally. I’ve achieved this but the onTouch scripts don’t work. How would i Fix this?

2 Likes

Can we see your current code and explorer?

2 Likes

This is the code that goes inside the egg:

local InsertEgg = workspace:WaitForChild("InsertEgg")

function onTouch(hit)
	local h = hit.Parent:findFirstChild("Humanoid")
	if h ~= nil then
		script.Parent:remove()
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		player.leaderstats.Eggs.Value = player.leaderstats.Eggs.Value + (player.leaderstats["Egg Multiplier"].Value) + 1
		InsertEgg:Fire()
	end
end
script.Parent.Touched:Connect(onTouch)
1 Like

So basically you are wanting to define it inside a local script preferably the one you are spawning it from and define the touch there

local player = game.Players.LocalPlayer
local part = Instance.new("Part",workspace)
part.Anchored = true
part.Touched:Connect(function()
	remoteevent:FireSever()
end)

you will have to connect it with a remote to make the player stats change but thats the basics of it

2 Likes

I don’t know what your doing because I can’t see your explorer or your current code.

1 Like

The eggs are in ReplicatedStorage with the scripts already in them

1 Like

1 Like

if you are spawning them locally then server scripts will not work for them and local scripts cannot work inside of parts

2 Likes

You can put the touch event on the server and send an event to the client where it can clone the egg.
On the server you can handle the playerstats
EDIT: Didn’t mean to reply to you sorry! :sweat_smile:

1 Like

This is prone to Exploiting, why have you decided to make eggs spawn and register touch locally?


https://developer.roblox.com/api-reference/class/LocalScript

https://developer.roblox.com/api-reference/class/Script

Even if a Script is a descendant of workspace if it doesn’t exist on the Server then it won’t run.

4 Likes

Im trying to spawn them locally so i can make a gamepass that doubles the chances of better types of eggs spawning

1 Like

An Exploiter can teleport all the eggs to them (or the other way around) and even choose to spawn only high value eggs if it’s controlled by a Localscript.


May I suggest that you change it a little bit, instead of increasing the chance of spawning high value eggs, you can let the players collect eggs but there is a chance that they might get an additional egg that is better.

Doing this prevents exploiters from getting High value eggs for free but it doesn’t prevent teleporting eggs to them or teleporting to eggs.

You can handle touch events on the server, and have a script that checks if a player is teleporting.

4 Likes

@RuizuKun_Dev is correct, handling the eggs on the client is very unsecure.

I would recommend spawning the eggs on the server and then deleting certain eggs for certain clients on the client side of things, this way only the eggs that are suppose to be seen by certain users can be seen. Then if an exploiter makes these eggs visible again simply check and see on the server if the egg belongs to that user. If it does then they can claim it, if not then you know the user is exploiting and you can do some sort of punishment.

I’m sure there are better ways but this is what comes to mind for me :smiley:

6 Likes

you could just have eggs spawn without a type and then randomly choose the type onTouch. this way you can check if theuser hasthe gamepass and then improve their odds.

2 Likes