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)
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)
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"