Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to set values on players serverside?

when a player in my game clicks a button in a menu in my game it sets a string value on the player to the equipped value of the button i named the value eq like if the player equips the color blue the value will be set to Blue. however its in a local script and only sets it client side. but if i change it to a regular script it dosent work at all and my game script reads the players eq value on the server side to give them the blue weapon.

Script on button:

local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
	if player.Shop.Red.Value == true then
	player.eq.Value = "Red"
	end
end)

Code in game script:

for _, player in pairs (game.Players:GetPlayers()) do
                        if player.eq.Value == "Red" then
			local primary = game.ReplicatedStorage.Red 
			local newprimary = primary:Clone()
			newprimary.Parent = player.Backpack
			end
			if player.eq.Value == "Yellow" then
			local primary = game.ReplicatedStorage.Yellow  
			local newprimary = primary:Clone()
			newprimary.Parent = player.Backpack
			end
			if player.eq.Value == "Blue" then
			local primary = game.ReplicatedStorage.Blue  
			local newprimary = primary:Clone()
			newprimary.Parent = player.Backpack
			end
			if player.eq.Value == "Green" then
			local primary = game.ReplicatedStorage.Green  
			local newprimary = primary:Clone()
			newprimary.Parent = player.Backpack
			end
			if player.eq.Value == "None" then
			local primary = game.ReplicatedStorage.Sword  
			local newprimary = primary:Clone()
			newprimary.Parent = player.Backpack
			end				
		end

2 comments

0

Use remote events. Fire from local

1 answer

✓ Accepted answer

Step 1: Create remote event named changeEq(or whatever name you want, just change the script) in Replicated Storage. Step 2: Create a local script to signal the event to fire Step 3: Create the server script to run when the event is fired

Local Script

local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
	if player.Shop.Red.Value == true then 
		local color = "Red"
		game.ReplicatedStorage.changeEq:FireServer(color)
	end
end)

Server Script

game.ReplicatedStorage.changeEq.OnServerEvent:Connect(function(player, color)
	player.eq.Value = color
end)

1 comment

0

Thanks so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.