OnClientEvent dont work, Need help fixing my script (Beginner)

1. What do you want to achieve?
So i want my script to fire a remote event named ButtonActivated, and then create another script that will work when the event is fired.

2. What is the issue?
my issue is that the function that is suppose to work when the event fires doesnt work, the script that fires the event works fine, i think that the issue is something with OnClientEvent

3. What solutions have you tried so far?
ive tried many solution, switch between local scripts and normal scripts and use different commands, and it just doesnt seem to work, its not the first time im having issues with remote events, the script i have so far is really simple yet i cant understand what im doing wrong

this is the first script located inside a part that is inside the workspace

local repStorage = game:GetService("ReplicatedStorage")
local buttonEvent = repStorage.ButtonActivated
local buttonPart = script.Parent
local playerSer = game:GetService("Players")

buttonPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local PlayerName = game.Players:GetPlayerFromCharacter(hit.Parent)
		buttonEvent:FireClient(PlayerName)
		print(PlayerName)
		task.wait(1)
	end
end)

this is a local script located inside Server Script Service

local repStorage = game:GetService("ReplicatedStorage")
local ButtonEvent = repStorage:WaitForChild("ButtonActivated")

ButtonEvent.OnClientEvent:Connect(function(plr)

	plr.leaderstats.Points.Value += 10
	warn("Pinged normal script")

end)

Ok Im going to tell you this rn, never, and I mean never, change data on the client, which btw is what you are doing. By handling data, or your leaderstats via the client you are basically allowing exploiters to give themself how ever many points they want.

Not to mention you wont be able to save data if you do it via the client, as most changes to the client do not replicate to the server.

Instead assuming that you first script is a script and not a local script you can do this:

buttonPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
        Player.leaderstats.Points.Value += 10
	end
end)

All this does is adds the points via the script, so theres no point for another one

okay first of all thanks for the tip! but i need the second script (the local script) to be in server script service because i want to make a ui after you touch the button, is there anything wrong with my first 2 scripts?
also, if exploiters can change normal scripts that are inside the workspace, wouldnt they be able to change the new script that you suggested too?

Make a local script (Inside StarterGui, StarterPlayerScripts, StarterCharacterScripts) that when you click a button fire a remote event to the server and handle what you want on the server script.

and for the other scripts you can put them inside one script as @FroDev1002 showed

exploiters cannot change client/local scripts, server scripts inside a server environment don’t replicate to any client

nope! See exploiters will be unable to change stuff inside of a server script depending on where it is parented, or just in general.

So first off your main touch script should probably be parented via server script service. Then if you wanted to have UI I recommend highly, that you parent the UI to starterGui. Then put a local script in said UI, and thats how you’d control it.

One thing I’ve noticed from your messages, is that you seem to not understand the server-to-client model, and hey I dont blame you its complicated. So heres the docs on it to hopefully help:

If you have any questions feel free to ask

1 Like

alright ill check it out, thanks fro!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.