Detecting a touch with a local script

I’m new to scripting and I have some trouble making a sell area for my game. I have a local script inside the part(sell area) that fires a remote event to change the player’s money/leaderstats. The event isn’t firing and I think it’s because touch detection isn’t working. Here is the local script code:

local sell = script.Parent
local rs = game:GetService(“ReplicatedStorage”)
local sellevent = rs:WaitForChild(“Sell”)

sell.Touched:Connect(function(hit)
print(“touch deceted”)
if hit.Parent == game.Players.LocalPlayer.Character then
sellevent.FireServer()
print(“eventFired”)
end
end)

and the code with the leaderstats which is in StarterPack:

local replicatedstorage = game:GetService(“ReplicatedStorage”)
local remotedata = game:GetService(“ServerStorage”):WaitForChild(“RemoteData”)
local cooldown = 0.5
local sellevent = replicatedstorage:WaitForChild(“Sell”)
replicatedstorage.Remotes.Syrup.OnServerEvent:Connect(function(player)
if not remotedata:FindFirstChild(player.name) then return “NoFolder” end
local debounce = remotedata[player.Name].Debounce
if not debounce.Value then
debounce.Value = true

	player.leaderstats.Syrup.Value = player.leaderstats.Syrup.Value + 1 *(player.leaderstats.Sacrfices.Value + 1)
	
	
	wait(cooldown)
	debounce.Value = false
	
	sellevent.OnServerEvent:Connect(function(player)
		player.leaderstats.Syrup.Value += player.leaderstats.Money.Value
		player.leaderstats.Syrup.Value = 0
		print("event found")
	end)
	end

end)

I would be thankful for any help

1 Like

When firing events, dont use “sellevent.FireServer()” you would need to use a semicolon instead of the period, such as “sellevent:FireServer()”

But tbh, its probably better overall if you just handle the touch event on the server in the very beginning, making it less vulnerable to infinite money expoits and possibly saving you from using a remote event.

local script in workspace wont work instead of a local try using a normal script add a script in the part and write this:

local sell = script.Parent


sell.Touched:Connect(function(hit)
print(“touch deceted”)
if game.Players:GetPlayerFromCharacter(hit) then
    local plr = game.Players:GetPlayerFromCharacter(hit)
         plr.leaderstats.Syrup.Value += player.leaderstats.Money.Value
	---player.leaderstats.Syrup.Value = 0
	print("finish giving money")

  end
 end)
1 Like

LocalScripts aren’t able to be ran in the workspace, what you could do is call the object in the workspace from a place where it can run.

1 Like

Sorry I hate to be one of those people but how would I do something like this? My understanding is that when you use a path in a local script you have to define it using parents and children. So how would I call with a local script if they run in workspace?

You can just do

Variable_Name = workspace.Instance_Name_Here

For example if you wanted to use a click detector:

local click_detector = workspace.part.ClickDetector

click_detector.MouseClick:Connect(function()
    print("do stuff")
end)

Not sure if this will entirely work for click detectors but I used this inside of my game for a proximity prompt for a camera system.