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

Why won't this local + server stun script work?

So I have a script in StarterPack, that checks when a stun value is true, and then when the value is true, it uses a remote event to do my stun function, however, when the stun value is true, the script never activates and i'm not sure why.

Local Script:

local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local root = Character:WaitForChild("HumanoidRootPart")
local stunned = Character:WaitForChild("Stunned")
local stuntime = 1


repeat wait() until stunned
if stunned.Value == true then
		script.RemoteEvent:FireServer(Player, stunned, root)
	end

Remote event:

local Cooldown = false
local stuntime = 1

script.Parent.OnServerEvent:Connect(function(Player, stunned, root)
	root.Anchored = true
	wait(stuntime)
	root.Anchored = false
	stunned.Value = false
end)

2 answers

✓ Accepted answer

Your localscript is saying "wait until the stunned value exists" when it does exist, it checks if the stunned value is true, then if it is it fires the server.

Instead, we want to use the .Changed event to run a funciton every time the stunned value changes:

local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local root = Character:WaitForChild("HumanoidRootPart")
local stunned = Character:WaitForChild("Stunned")
local stuntime = 1

stunned.Changed:Connect(function()
	if stunned.Value == true then
		script.RemoteEvent:FireServer(Player)
	end
end

You also don't need to pass the stunned value/the humanoid root part through the remote event. Just get it from the character:

local Cooldown = false
local stuntime = 1

script.Parent.OnServerEvent:Connect(function(Player)
	local character = player.Character
	assert(character, "character doesnt exist") --error if the character doesnt exist
	local stunned = player.Character.Stunned

	character.HumanoidRootPart.Anchored = true
	wait(stuntime)
	character.HumanoidRootPart.Anchored = false
	stunned.Value = false
end)

#Troubleshoot:

  • Check for any errors in the output, if there are any errors, send them here.
  • See if the localscript is running, print("localscript is running") at the top of the localscript
  • See if the server script is running: print("script is running") at the top of the script
  • confirm that the remote is the script.Parent in the server script
  • make sure that the event is running by printing the player inside of the onserverevent: print(Player)

6 comments

1

let me know if you still have problems after trying the new code / following the advice in troubleshoot list. it's ok if u do. I just want to make sure you get your code working.

0

Thanks a lot for the help. I was able to get the scripts working up until the last line of the server script, the "stunned.Value = false", the character gets rooted, but the stun value never turns off.

0

Ah, that's because we never defined stunned. My bad. I will do that right now.

0

see my edit please

0

Thank you so much, the script works.

0

Cheers

Pretty common problem but it's because scripts run up and down once.

Summary from your local script:

local a=1;

if a==1 then--since a==1 then the if statement code is ran and the print function is printed, however notice how it only prints once. this means the script doesnt go back to that if statement more than once unless, you know, loops or functions. therefore if a~=1 then it would be impossible to print 'wow potato' from the line below
	print'wow potato';
end;

I suggest an event on the value, like .Changed so that the value can change anytime and the if statement can be repeatedly ran.

workspace.Changed:Connect(function()
	print(workspace.Name); 
end;
wait(1);
workspace.Name='wow potato';-->event was fired "wow potato"
wait(1);
workspace.Name='wow potato 2';-->event was fired "wow potato 2"

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